//
//  MYShardAlertSheet.m
//  MuYing
//
//  Created by mac on 2020/6/5.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "MYShardAlertSheet.h"
#import <WXApi.h>
#import <FBSDKShareKit.h>

@interface MYShardAlertSheet ()<FBSDKSharingDelegate>
@property (nonatomic,retain) NSMutableArray *imageAry;
@property (nonatomic,retain) NSMutableArray *titleAry;
@end

@implementation MYShardAlertSheet

- (instancetype)init
{
    if (self=[super init]) {
        self.preferredStyle=TEAlertControllerStyleActionSheet;
        self.preferredContentSize=CGSizeMake(kScreenWidth, 250*KSRatio+kHeight_Botton_SafeArea);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initializeDataSource];
    [self initializeUI];
}

- (void)initializeDataSource
{
    self.imageAry=[NSMutableArray arrayWithArray:@[@"Alret／share／wechat／normal",@"Alret／share／wechat／normal Copy",@"icons_community_fr_normal",@"icons_community_wb_normal",@"Alret／share／wechat／normal(1)",@"Alret／share／wechat／normal Copy(1)",@"编组",@"icons_community_wb_normal(1)"]];
    self.titleAry=[NSMutableArray arrayWithArray:@[kYQLS(@"复制链接"),kYQLS(@"微信"),kYQLS(@"朋友圈"),@"Facebook",@"WhatApp",@"Line",@"Instagram",@"Twitter"]];
}

- (void)initializeUI
{
    self.view.backgroundColor=UIColor.whiteColor;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,kScreenWidth, 250*KSRatio+kHeight_Botton_SafeArea) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(16*KSRatio,16*KSRatio)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame =CGRectMake(0, 0, kScreenWidth, 250*KSRatio+kHeight_Botton_SafeArea);
    maskLayer.path = maskPath.CGPath;
    self.view.layer.mask = maskLayer;
    int index=0;
    for (int row=0; row<2; row++) {
        for (int section=0; section<4; section++,index++) {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag=789+index;
            btn.frame=CGRectMake(20*KSRatio+section*84*KSRatio, 45*KSRatio+row*93*KSRatio, 84*KSRatio, 93*KSRatio);
            [btn setImage:[UIImage imageNamed:self.imageAry[index]] forState:UIControlStateNormal];
            [btn setTitle:self.titleAry[index] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitleColor:kHexString(@"#989898") forState:UIControlStateNormal];
            btn.titleLabel.font=kFont_Regular(10);
            [btn setAdjustsImageWhenHighlighted:NO];
            [self setUpButtonTitleAndImageLocation:btn space:0];
            [self.view addSubview:btn];
        }
    }
}

// 设置按钮图片与标题位置
- (void)setUpButtonTitleAndImageLocation:(UIButton *)button space:(CGFloat)space {
    CGSize imageSize = button.imageView.image.size;
    CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    button.contentVerticalAlignment=UIControlContentVerticalAlignmentTop;
    button.imageEdgeInsets=UIEdgeInsetsMake(0,(button.width-imageSize.width)/2, 0,0);
   button.titleEdgeInsets=UIEdgeInsetsMake(imageSize.height+space,-imageSize.width+((button.width-titleSize.width)/2), 0,0);
}



- (void)btnAction:(UIButton*)sender
{
    NSInteger index=sender.tag-789;
    NSString *title=self.titleAry[index];
    if ([title isEqualToString:kYQLS(@"微信")]) {
        
    }
    else if ([title isEqualToString:kYQLS(@"朋友圈")])
    {
        
        UIImage *image=[UIImage imageNamed:@"编组"];
           
        WXImageObject *imageObject = [WXImageObject object];
        imageObject.imageData =UIImagePNGRepresentation(image);

        WXMediaMessage *message = [WXMediaMessage message];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"gengduo"
                                                             ofType:@"png"];
        message.thumbData = [NSData dataWithContentsOfFile:filePath];
        message.mediaObject = imageObject;

        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneTimeline;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }
    else if ([title isEqualToString:@"Facebook"])
    {
        FBSDKShareLinkContent *linkContent = [[FBSDKShareLinkContent alloc] init];
           linkContent.contentURL = [NSURL URLWithString:@"https://image.baidu.com"];
           [FBSDKShareDialog showFromViewController:self withContent:linkContent delegate:self];
    }
    else if ([title isEqualToString:@"Line"])
    {
        
    }
    else if ([title isEqualToString:@"Instagram"])
    {
        
    }
    else if ([title isEqualToString:@"Twitter"])
    {
        
    }
    else if ([title isEqualToString:@"WhatApp"])
    {
        
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - FBSDKSharingDelegate
 -(void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary<NSString *, id> *)results{
     
 }

- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
{
    
}
 
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer
{
    
}

@end
